package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.admin.statement.pojo.GoodsStat;
import com.cskaoyan.bean.admin.statement.pojo.OrderStat;
import com.cskaoyan.bean.admin.statement.pojo.UserStat;
import com.cskaoyan.bean.admin.statement.vo.StatGoodsVo;
import com.cskaoyan.bean.admin.statement.vo.StatOrderVo;
import com.cskaoyan.bean.admin.statement.vo.StatUserVo;
import com.cskaoyan.bean.admin.statement.vo.StatementVo;
import com.cskaoyan.bean.pojo.UserExample;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.admin.StatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Name : StatementServiceImpl.java
 * @Time : 2021/8/13 14:33
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@Service
public class StatementServiceImpl implements StatementService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Override
    public StatementVo getOrders() {
        List<OrderStat> orderStats = orderMapper.selectByGroup();
        List<StatOrderVo> statOrderVos = new ArrayList<>();
        for (OrderStat orderStat : orderStats) {
            StatOrderVo statOrderVo = new StatOrderVo();
            BigDecimal amount = orderStat.getAmount();
            Integer count = orderStat.getOrders();
            BigDecimal pcr = amount.divide(BigDecimal.valueOf(count),BigDecimal.ROUND_HALF_UP);
            statOrderVo.setPcr(pcr);
            statOrderVo.setDay(orderStat.getDay());
            statOrderVo.setAmount(orderStat.getAmount());
            statOrderVo.setCustomers(orderStat.getCustomers());
            statOrderVo.setOrders(count);
            statOrderVos.add(statOrderVo);
        }
        Class<StatOrderVo> statOrderVoClass = StatOrderVo.class;
        Field[] fields = statOrderVoClass.getDeclaredFields();
        List<String> strings = new ArrayList<>();
        for (Field field : fields) {
            strings.add(field.getName());
        }
        return StatementVo.create(strings,statOrderVos);
    }

    @Override
    public StatementVo getUsers() {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        Long count = userMapper.countByExample(userExample);
        List<UserStat> userStats = userMapper.selectByGroup();
        List<StatUserVo> statUsers = new ArrayList<StatUserVo>();
        for (UserStat userStat : userStats) {
            StatUserVo statUserVo = new StatUserVo();
            statUserVo.setDay(userStat.getDay());
            statUserVo.setUsers(userStat.getUsers());
            statUsers.add(statUserVo);
        }

        Class<StatUserVo> statUserVoClass = StatUserVo.class;
        Field[] declaredFields = statUserVoClass.getDeclaredFields();
        List<String> strings = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            strings.add(declaredField.getName());
        }
        return StatementVo.create(strings,statUsers);
    }

    @Override
    public StatementVo getGoods() {
        List<GoodsStat> goodsStats = orderGoodsMapper.selectByGroup();
        List<StatGoodsVo> statGoodsVos = new ArrayList<>();
        for (GoodsStat goodsStat : goodsStats) {
            StatGoodsVo statGoodsVo = new StatGoodsVo();
            statGoodsVo.setAmount(goodsStat.getAmount());
            statGoodsVo.setDay(goodsStat.getDay());
            statGoodsVo.setOrders(goodsStat.getOrders());
            statGoodsVo.setProducts(goodsStat.getProducts());
            statGoodsVos.add(statGoodsVo);
        }

        Class<StatGoodsVo> statGoodsVoClass = StatGoodsVo.class;
        Field[] declaredFields = statGoodsVoClass.getDeclaredFields();
        List<String> strings = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            strings.add(declaredField.getName());
        }
        return StatementVo.create(strings,statGoodsVos);
    }
}
